#include"process.h"
#include<string.h>
#include<unistd.h>

#define size 101
#define STYLE '#'

//v1:实现进度条基本功能
void process()
{
   int rate=0;
   char buffer[size];
   memset(buffer,0,sizeof(buffer));
   const char *lable ="|/-\\";
   
   int len=strlen(lable);

   while(rate<=100)
   {
      printf("[%-100s][%d%%][%c]\r",buffer,rate,lable[rate%len]);
      fflush(stdout);
      buffer[rate] = STYLE;
      rate++;

      usleep(50000);
   }

   printf("\n");
}

//v2:根据进度动态刷新一次进度条
void FlushProcess(const char *tips,double total,double current)
{
   const char *lable = "|/-\\";
   static int index=0;
   int len = strlen(lable);
   char buffer[size];
   memset(buffer,0,sizeof(buffer));

   //int num=(int)(current*100/total);
   double rate=current*100.0/total;
   int num = (int) rate;
  
   int i=0;
   for(i=0;i<num;i++)
   {
      buffer[i]=STYLE;

   }

   printf("%s...[%-100s][%.1f%%][%c]\r",tips,buffer,rate,lable[index++]);
   fflush(stdout);
   index%=len;


   if(num==100){
      printf("\n");
   }
}
